package hidalgo.expenses.dao;

import hidalgo.expenses.entities.Transaction;
import hidalgo.expenses.utils.Util;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class TransactionDao extends BaseDao {

	public static String TRANSACTIONID = "transaction_id";
	public static String ISEXPENSE = "isexpense";
	public static String ACCOUNTID = "account_id";
	public static String DATE = "date";
	public static String AMOUNT = "amount";
	public static String PAYMENTMETHODID = "paymentmethod_id";
	public static String IMAGE = "image";
	public static String DESCRIPTION = "description";

	public static ArrayList<Transaction> getTransactionByAccount(int accountId) {

		MyHttpClient conection = new MyHttpClient(URLSELECT);

		conection.addDataToSend("queryphp", "SELECT * FROM transaction WHERE "
				+ ACCOUNTID + "='" + accountId + "' ");

		JSONObject jsonObjRecv = conection.executeHttpPost();

		if (jsonObjRecv != null) {
			try {
				JSONArray resultados = jsonObjRecv.getJSONArray("resultados");

				if (resultados != null) {

					ArrayList<Transaction> transactionList = new ArrayList<Transaction>();

					for (int i = 0; i < resultados.length(); i++) {

						JSONObject elemento = (JSONObject) resultados.get(i);

						Transaction transaction = new Transaction(
								elemento.getInt(ISEXPENSE));

						transaction.setTransactionId(elemento
								.getInt(TRANSACTIONID));
						transaction.setAccountId(elemento.getInt(ACCOUNTID));
						transaction
								.setDate(Util.getDateFromString(
										elemento.getString(DATE),
										Util.SQL_DATE_FORMAT));
						transaction.setAmount(elemento.getDouble(AMOUNT));
						transaction.setPaymentMethodId(elemento
								.getInt(PAYMENTMETHODID));
						transaction.setDescription(elemento
								.getString(DESCRIPTION));
						// image field is not set yet

						transactionList.add(transaction);

					}

					resultados = null;

					return transactionList;
				} else {
					return null;
				}
			} catch (JSONException e) {
				e.printStackTrace();
				return null;
			}
		} else {
			return null;
		}
	}

	public static boolean insert(Transaction transaction) {

		Boolean success = false;

		MyHttpClient conection = new MyHttpClient(URLINSERT_UPDATE_DELETE);

		conection.addDataToSend(
				"queryphp",
				"INSERT INTO transaction " + "("
						+ ISEXPENSE
						+ ","
						+ ACCOUNTID
						+ ","
						+ DATE
						+ ","
						+ AMOUNT
						+ ","
						+ PAYMENTMETHODID
						+ ","
						+ DESCRIPTION
						+ ") "
						+ " VALUES('"
						+ (transaction.isExpense() ? 1 : 0)
						+ "','"
						+ transaction.getAccountId()
						+ "','"
						+ Util.getStringFromDate(transaction.getDate(),
								Util.SQL_DATE_FORMAT) + "','"
						+ transaction.getAmount() + "','"
						+ transaction.getPaymentMethodId() + "','"
						+ transaction.getDescription() + "')");

		JSONObject jsonObjRecv = conection.executeHttpPost();

		if (jsonObjRecv != null) {
			try {
				success = jsonObjRecv.getBoolean("resultados");
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		return success;
	}
}